using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Models.BUS
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
    using TemplateWeb.DTO;
    using TemplateWeb.DAO;
    using System.Data;
	#endregion
	public class PhienDauGiaBUS
	{
		public List<PhienDauGiaDTO> SelectAll()
		{
			try
			{
                var lstDto = new List<PhienDauGiaDTO>();
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var objDS = rpt.SelectAll();
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(PhienDauGiaDTO.Translate(row));
                    }
                }
                return lstDto;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("PhienDauGia - SelectAll: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("PhienDauGia - SelectAll: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<PhienDauGiaDTO>();
		}

        public PhienDauGiaDTO SelectByID(int id)
		{
			try
			{
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var objDS = rpt.SelectByID(id);
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    return PhienDauGiaDTO.Translate(objDS.Tables[0].Rows[0]);
                }
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("PhienDauGia - SelectByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("PhienDauGia - SelectByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return null;
		}

        public bool Insert(PhienDauGiaDTO obj)
		{
			try
			{
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                return rpt.Insert(obj);
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("PhienDauGia - Insert: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("PhienDauGia - Insert: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return false;
		}

		public bool Delete(int id)
		{
			try
			{
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                return rpt.Delete(id);
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("PhienDauGia - Delete: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("PhienDauGia - Delete: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return false;
		}

		public bool Update(PhienDauGiaDTO obj)
		{
			try
			{
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                return rpt.Update(obj);
			}
			catch
			{
				return false;
			}
		}

        public List<PhienDauGiaDTO> SelectByIDHinhThucDauGia(int idhinhthucdaugia)
		{
			try
			{
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var lstDto = new List<PhienDauGiaDTO>();
                var objDS = rpt.SelectByIDHinhThucDauGia(idhinhthucdaugia);
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(PhienDauGiaDTO.Translate(row));
                    }
                }
                return lstDto;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("PhienDauGia - SelectByIDHinhThucDauGia: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("PhienDauGia - SelectByIDHinhThucDauGia: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<PhienDauGiaDTO>();
			}
		}

        public List<PhienDauGiaDTO> SelectByIDDanhMucCon(int iddanhmuccon)
		{
			try
			{
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var lstDto = new List<PhienDauGiaDTO>();
                var objDS = rpt.SelectByIDDanhMucCon(iddanhmuccon);
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(PhienDauGiaDTO.Translate(row));
                    }
                }
                return lstDto;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("PhienDauGia - SelectByIDDanhMucCon: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("PhienDauGia - SelectByIDDanhMucCon: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<PhienDauGiaDTO>();
			}
		}

        public List<PhienDauGiaDTO> RetrieveByID(int id)
        {
            try
            {
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var lstDto = new List<PhienDauGiaDTO>();
                var objDS = rpt.SelectByID(id);
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(PhienDauGiaDTO.Translate(row));
                    }
                }
                return lstDto;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("PhienDauGia - SelectByIDDanhMucCon: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("PhienDauGia - SelectByIDDanhMucCon: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<PhienDauGiaDTO>();
            }
        }

        public List<PhienDauGiaDTO> SelectByIDDanhMucChinh(int iddanhmucchinh)
        {
            try
            {
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var lstDto = new List<PhienDauGiaDTO>();
                var objDS = rpt.SelectByIDDanhMucChinh(iddanhmucchinh);
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(PhienDauGiaDTO.Translate(row));
                    }
                }
                return lstDto;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("PhienDauGia - SelectByIDDanhMucChinh: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("PhienDauGia - SelectByIDDanhMucChinh: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<PhienDauGiaDTO>();
            }
        }

        public List<PhienDauGiaDTO> SelectByIDNguoiDang(Guid idnguoidang)
		{
			try
			{
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var lstDto = new List<PhienDauGiaDTO>();
                var objDS = rpt.SelectByIDNguoiDang(idnguoidang);
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(PhienDauGiaDTO.Translate(row));
                    }
                }
                return lstDto;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("PhienDauGia - SelectByIDNguoiDang: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("PhienDauGia - SelectByIDNguoiDang: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<PhienDauGiaDTO>();
			}
		}

        public List<PhienDauGiaDTO> SelectByIDNguoiDauGia(Guid idnguoidaugia)
        {
            try
            {
                var lstItem = new List<PhienDauGiaDTO>();
                var lstDto = new List<PhienDauGiaDTO>();

                // Lay danh sach tat ca cac phien dau gia
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var objDS = rpt.SelectAll();
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(PhienDauGiaDTO.Translate(row));
                    }
                }

                // Lap qua danh sach phien dau gia, lay ra duoc danh sach nguoi choi cua moi phien
                foreach (PhienDauGiaDTO phien in lstDto)
                {
                    var exist = false;
                    foreach (NguoiChoiDauGiaDTO nguoichoi in phien.NguoiChoiDauGia)
                    {
                        if (nguoichoi.IDNguoiDung == idnguoidaugia)
                        {
                            exist = true;
                            break;
                        }
                    }
                    if (exist)
                    {
                        lstItem.Add(phien);
                    }
                }

                return lstItem;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("PhienDauGia - SelectByIDNguoiDauGia: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("PhienDauGia - SelectByIDNguoiDauGia: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<PhienDauGiaDTO>();
            }
        }

        public List<PhienDauGiaDTO> SelectPhienThanhCong()
        {
            try
            {
                var lstItem = new List<PhienDauGiaDTO>();
                var lstDto = new List<PhienDauGiaDTO>();

                // Lay danh sach tat ca cac phien dau gia
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var objDS = rpt.SelectAll();
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(PhienDauGiaDTO.Translate(row));
                    }
                }

                // Lap qua danh sach phien dau gia, lay ra duoc danh sach phien dau gia thanh cong
                foreach (PhienDauGiaDTO phien in lstDto)
                {
                    // Xet cac phien dau gia da ket thuc
                    if (phien.ThoiGianKetThuc < DateTime.Now)
                    {
                        // Phien dau gia co nguoi choi thi xem nhu thanh cong
                        if (phien.NguoiChoiDauGia.Count > 0)
                        {
                            lstItem.Add(phien);
                        }
                    }           
                }

                return lstItem;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("PhienDauGia - SelectByIDNguoiDauGia: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("PhienDauGia - SelectByIDNguoiDauGia: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<PhienDauGiaDTO>();
            }
        }

        public List<PhienDauGiaDTO> SelectPhienThangCuoc(Guid idnguoidung)
        {
            try
            {
                var lstItem = new List<PhienDauGiaDTO>();
                var lstDto = new List<PhienDauGiaDTO>();

                // Lay danh sach tat ca cac phien dau gia
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var objDS = rpt.SelectAll();
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(PhienDauGiaDTO.Translate(row));
                    }
                }

                // Lap qua danh sach phien dau gia
                foreach (PhienDauGiaDTO phien in lstDto)
                {
                    var exist = false;
                    List<NguoiChoiDauGiaDTO> lstNguoiDauGia = new List<NguoiChoiDauGiaDTO>();

                    // Lay danh sach nguoi choi cho moi phien
                    if (phien.NguoiChoiDauGia.Count > 0)
                    {
                        foreach (NguoiChoiDauGiaDTO nguoichoi in phien.NguoiChoiDauGia)
                        {
                            // Add tat ca nguoi choi co tham gia phien dau gia vao list
                            lstNguoiDauGia.Add(nguoichoi);
                            if (nguoichoi.IDNguoiDung == idnguoidung)
                            {
                                exist = true;
                            }
                        }
                    }
                    // Chi xet cac phien dau gia co nguoi dung tham gia
                    if (exist)
                    {
                        if (phien.HinhThucDauGia.TenHinhThucDauGia.ToUpper().Contains("ANH"))
                        {
                            // Lay dau gia max cho moi phien
                            var giamax = lstNguoiDauGia.Max(n => n.GiaDuaRa);
                            // Lay danh sach cac gia dua ra boi nguoi dung
                            var lstGiaDoNguoiDungDuaRa = lstNguoiDauGia.Where(n => n.IDNguoiDung == idnguoidung).ToList();
                            // Lap qua danh sach nguoi choi dau gia
                            foreach (var daugia in lstGiaDoNguoiDungDuaRa)
                            {
                                // Neu gia do nguoi dung dua ra la MAX
                                if (daugia.GiaDuaRa == giamax)
                                {
                                    lstItem.Add(phien);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            // Lay dau gia max cho moi phien
                            var giamin = lstNguoiDauGia.Min(n => n.GiaDuaRa);
                            // Lay danh sach cac gia dua ra boi nguoi dung
                            var lstGiaDoNguoiDungDuaRa = lstNguoiDauGia.Where(n => n.IDNguoiDung == idnguoidung).ToList();
                            // Lap qua danh sach nguoi choi dau gia
                            foreach (var daugia in lstGiaDoNguoiDungDuaRa)
                            {
                                // Neu gia do nguoi dung dua ra la MAX
                                if (daugia.GiaDuaRa == giamin)
                                {
                                    lstItem.Add(phien);
                                    break;
                                }
                            }
                        }
                    }
                }
                return lstItem;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("PhienDauGia - SelectByIDNguoiDauGia: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("PhienDauGia - SelectByIDNguoiDauGia: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<PhienDauGiaDTO>();
            }
        }

        public List<PhienDauGiaDTO> TimKiem(string tieude, bool timtrongmota, string thumuc, string nguoiban, string batdausaungay, string ketthuctruocngay)
        {
            try
            {
                var lst = new List<PhienDauGiaDTO>();
                var lstPhienDauGia = new List<PhienDauGiaDTO>();
                
                var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
                var dsPhienDauGia = rpt.SelectAll();
                if (dsPhienDauGia != null && dsPhienDauGia.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in dsPhienDauGia.Tables[0].Rows)
                    {
                        lstPhienDauGia.Add(PhienDauGiaDTO.Translate(row));
                    }
                }

                // Tim kiem theo tieu de
                if (!string.Empty.Equals(tieude))
                {
                    if (timtrongmota)
                    {
                        foreach (var phien in lstPhienDauGia)
                        {
                            var mota = HtmlRemoval.StripTagsCharArray(phien.SanPham.MoTa);
                            if (phien.SanPham.TenSanPham.ToUpper().Contains(tieude.ToUpper()) || mota.ToUpper().Contains(tieude.ToUpper()))
                            {
                                lst.Add(phien);
                            }
                        }
                    }
                    else
                    {
                        lst = lstPhienDauGia.Where(
                                n => n.SanPham.TenSanPham.ToUpper().Contains(tieude.ToUpper())
                                ).ToList();
                    }
                }
                else // bo qua tieu chi tim kiem theo tieu de
                {
                    lst = lstPhienDauGia.ToList();
                }

                // Tim kiem theo thu muc
                if (!string.Empty.Equals(thumuc))
                {
                    var thumucComp = thumuc.Split(new char[] { '-' });
                    // Thu muc: Not all
                    if (!"-1".Equals(thumucComp[0]))
                    {
                        int danhmuc;
                        // Tim theo danh muc chinh
                        if ("0".Equals(thumucComp[0]))
                        {
                            danhmuc = int.Parse(thumucComp[1]);
                            lst = lst.Where(n => n.SanPham.DanhMucCon.DanhMucChinh.ID == danhmuc).ToList();
                        }
                        else // Tim theo danh muc con
                        {
                            danhmuc = int.Parse(thumucComp[1]);
                            lst = lst.Where(n => n.SanPham.DanhMucCon.ID == danhmuc).ToList();
                        }
                    }
                }

                // Nguoi ban: Not all
                if (!"-1".Equals(nguoiban))
                {
                    var idnguoiban = Guid.Parse(nguoiban);
                    lst = lst.Where(n => n.IDNguoiDang == idnguoiban).ToList();
                }

                // Bat dau sau ngay
                if (!string.Empty.Equals(batdausaungay))
                {
                    var batdausaungayVal = DateTime.MinValue;
                    if (DateTime.TryParse(batdausaungay, out batdausaungayVal))
                    {
                        lst = lst.Where(n => n.ThoiGianBatDau.Date > batdausaungayVal.Date).ToList();
                    }
                }

                // Ket thuc truoc ngay
                if (!string.Empty.Equals(ketthuctruocngay))
                {
                    var ketthuctruocngayVal = DateTime.MinValue;
                    if (DateTime.TryParse(ketthuctruocngay, out ketthuctruocngayVal))
                    {
                        lst = lst.Where(n => n.ThoiGianKetThuc.Date < ketthuctruocngayVal.Date).ToList();
                    }
                }

                return lst;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("PhienDauGia - TimKiem: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("PhienDauGia - TimKiem: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
            }
            return new List<PhienDauGiaDTO>();
        }
	}
}